package cn.edu.wynu.controller;

import cn.edu.wynu.pojo.Student;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student")
public class RESTfulController {
    @GetMapping("/")
    List<Student> getStudents() {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(1, "张三", "男"));
        students.add(new Student(2, "李四", "男"));
        return students;
    }

    @PostMapping("/")
    public List<Student> postUser(@RequestBody Student student) {
        // @RequestBody注解用来绑定通过http请求中application/json类型上传的数据
        ArrayList<Student> students = new ArrayList<>();
        students.add(student);
        System.out.println(students);
        return students;
    }

    @GetMapping("/{id}")
    public Student getStudent(@PathVariable Integer id) {
        return new Student(id, "张三", "男");
    }

    @PostMapping("/{id}")
    public List<Student> putStudent(@PathVariable Integer id, @RequestBody Student student) {
        ArrayList<Student> students = new ArrayList<>();
        student.setId(id);
        students.add(student);
        return students;
    }

    @DeleteMapping("/{id}")
    public List<Student> deleteStudent(@PathVariable Integer id) {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(1, "张三", "男"));
        students.add(new Student(2, "李四", "男"));
        students.removeIf(student -> student.getId().equals(id));
        return students;
    }
}
